﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace Wild2.Models.Notifies
{
    public class Messages
    {
        public static int GetCountNewMessages(int UserID)
        {
            int tid = -1;
            //using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnect"].ConnectionString))
            //{
            //    SqlCommand cmd = new SqlCommand(@"SELECT COUNT(ID) as CNT FROM [Notifies] WHERE ([OwnerUserID] = @UID) and ([isRead] = 'FALSE')", conn);
            //    cmd.Parameters.Add("@UID", SqlDbType.Int).Value = UserID;
            //    cmd.Connection.Open();
            //    SqlDataReader reader = cmd.ExecuteReader();
            //    while (reader.Read())
            //    {
            //        tid = Convert.ToInt32(reader["CNT"]);
            //    }
            //    reader.Close();
            //    cmd.Connection.Close();
            //}
            return tid;
        }

        public static List<NotifyData> GetMessages(int UserID, int page)
        {
            List<NotifyData> notifies = new List<NotifyData>();
            //using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnect"].ConnectionString))
            //{
            //    SqlCommand cmd = new SqlCommand(@"SELECT TOP(30) [ID], [Type], [KtoID], [KtoNick], [WhereID], [Text], [Date], [isRead] FROM [Notifies] WHERE ([OwnerUserID] = @UID) ORDER BY [isRead], [Date] DESC", conn);
            //    cmd.Parameters.Add("@UID", SqlDbType.Int).Value = UserID;
            //    cmd.Connection.Open();
            //    SqlDataReader reader = cmd.ExecuteReader();
            //    while (reader.Read())
            //    {
            //        NotifyData notifyData = new NotifyData();
            //        notifyData.ID = Convert.ToInt32(reader["ID"]);
            //        notifyData.Type = reader["Type"].ToString();
            //        if (reader["KtoID"] != DBNull.Value) notifyData.KtoID = Convert.ToInt32(reader["KtoID"]);
            //        if (reader["KtoNick"] != DBNull.Value) notifyData.KtoNick = reader["KtoNick"].ToString();
            //        if (reader["WhereID"] != DBNull.Value) notifyData.WhereID = Convert.ToInt32(reader["WhereID"]);
            //        notifyData.Text = reader["Text"].ToString();
            //        notifyData.Date = Convert.ToDateTime(reader["Date"]);
            //        notifyData.isRead = Convert.ToBoolean(reader["isRead"]);
            //        notifies.Add(notifyData);
            //    }
            //    reader.Close();
            //    cmd.Connection.Close();
            //}
            //using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnect"].ConnectionString))
            //{
            //    SqlCommand cmd = new SqlCommand(@"UPDATE [Notifies] SET [isRead] = 'TRUE' WHERE ([OwnerUserID] = @UID)", conn);
            //    cmd.Parameters.Add("@UID", SqlDbType.Int).Value = UserID;
            //    cmd.Connection.Open();
            //    cmd.ExecuteNonQuery();
            //    cmd.Connection.Close();
            //}
            return notifies;
        }
    }
}